--------------------------------------------------------------------------------
-- hlp_atr.lua
--   smartcard development help toolset for ISO7816-3 ATR information
--
-- y.volta ET gmail.com
-- 2011/05/29 17:58:52 
--------------------------------------------------------------------------------


help.ATR = {}

local s_iso7816_atr = [[

|T0| |TA1|TB1|TC1|TD1|TA2|TB2|TC2| ... |T1|...|Tk||TCK|

Format byte
-  T0: Format byte T0 (mandatory), Encodes Y1 and K 

Interface bytes (optional)
- TA1:  Global, encodes Fi and Di 
- TB1:  Global, deprecated (x)
- TC1:  Global, encodes N (for T1.CGT)
- TD1:  Structural, encodes Y2 and T 
- TA2:  Global, specific mode byte 
- TB2:  Global, deprecated (x)
- TC2:  Specific to T=0 (calc WT)
- TD2:  Structural, encodes Y3 and T 

For i > 2, 
:: TDi-1:  Structural, encodes Yi and T 
- TAi, TCi, TDi: Specific to T after T from 0 to 14 in TDi-1 
                 Global after T=15 in TDi-1 
- TDi  Structural, encodes Yi+1 and T 

Historical bytes (optional)
  T1, T2, ... Tk      See ISO/IEC 7816-4

TCK   Check byte TCK (conditional) 

--------------------------------------------- Coding of T0 

| TD | TC | TB | TA |
| B8 | B7 | B6 | B5 || B4 | B3 | B2 | B1 |
|<------- Y1 ------>||<------- K ------->|

--------------------------------------------- Coding of TA1.Fi

| B8-B5 | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 |
|  Fi   |  372 |  372 | 558  |  744 | 1116 | 1488 | 1860 |  RFU |
|   f   |    4 |    5 |   6  |    8 |   12 |   16 |   20 |  〞  |

| B8-B5 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
|  Fi   |  RFU |  512 | 768  | 1024 | 1536 | 2048 |  RFU |  RFU |
|   f   |  --  |    5 |  7,5 |   10 |   15 |   20 |   〞 |  〞  |

--------------------------------------------- Coding of TA1.Di

| B4-B1 | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 |
|  Di   |  RFU |    1 |    2 |    4 |    8 |   16 |   32 |   64 |

| B4-B1 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
|  Di   |   12 |   20 |  RFU |  RFU |  RFU |  RFU |  RFU |  RFU |

--------------------------------------------- Coding of TA2

| B8 | B7 | B6 | B5 || B4 | B3 | B2 | B1 |
   |    |    |    | ||<------- T ------->|
   |    |    |    +------------------------- Fi, Di: 0-TA1, 1-default
   |    +----+------------------------------ Reserved.
   +---------------------------------------- ability for changing the 
                                             negotiable/specific mode:
                                             0 - yes, 1 - no
 [NOTE] An interface device supporting the three values F, D and T referenced 
 by TA2 should initiate the transmission protocol T with F and D. Otherwise, it 
 should perform either a warm reset (bit 8 set to 0) or a deactivation (bit 8 
 set to 1).
 
]]

setmetatable(help.ATR, 
{__tostring = function () return s_iso7816_atr; end }
)

--------------------------------------------------------------------------------
-- 解析 ATR 信息，需要 lscard 包支持
--------------------------------------------------------------------------------
function help.ATR.decode (sATR)
  if lscard == nil then 
    print ("[ERROR] lscard package required to decode ATR.\n");
    return;
  end
  
  print (sATR);
end

return help.ATR;
